package com.acme;

import java.util.ArrayList;
import java.util.EnumSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Player {

	public static final String PLAYER_ID = "player_id";
	
	public static enum STATUS {Active, Retired, Unknown}
	public static enum POSITION {
        QB ("QB"), NA ("?"), WR ("WR"), RB("RB"), FB("FB"), TE("TE"), OL("OL"), DL("DL"), LB("LB"), DB("DB"), K("K"), P("P"), ST("ST");
        private String value;

        POSITION(String value){
           this.value = value;
        }

        public String toString(){
            return value;
        }

        public static POSITION getByValue(String value){
            POSITION returnValue = null;
            for (final POSITION element : EnumSet.allOf(POSITION.class)) {
                if (element.toString().equals(value)) {
                    returnValue = element;
                }
            }
            return returnValue;
        }
	}
	
	public String[] positions;
	public String id;
	public String firstName;
	public String hometown;
	public String weight;
	public String height;
	public String nickname;
	public String dob;
	public String college;
	public String lastName;
	public String[] teams;
	public String drafted;
	
	private String name;
	private Player.STATUS status;
	//private Player.POSITION position;
	private String school;

	public static Player getPlayer(String JSONplayer){
		try {
			return new Player(new JSONObject(JSONplayer));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<Player> getPlayers(String JSONplayers){
		try {
			return getPlayers(new JSONArray(JSONplayers));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<Player> getPlayers(JSONArray players){
		
		ArrayList<Player> playerList = new ArrayList<Player>();
		
		try {
			int length = players.length();
			for (int i = 0; i < length; i++){
				JSONObject player = players.getJSONObject(i);
				playerList.add(new Player(player));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return playerList;
	}
	
	public Player(){
		positions = new String[1];
		positions[0] = "";
		id = "";
		firstName = "";
		hometown = "";
		weight = "";
		height = "";
		nickname = "";
		dob = "";
		college = "";
		lastName = "";
		teams = new String[1];
		teams[0] = "";
		drafted = "";
	}

	public Player(JSONObject player){
		
		String nullStr = "null";
		
		try {
			positions = this.parsePositions(player.getString("position"));
			id = player.getString("id");
			firstName = player.getString("first_name");
			hometown = player.getString("hometown");
			weight = player.getString("weight");
			height = player.getString("height");
			
			nickname = player.getString("nickname");
			if (nickname.equals(nullStr)){
				nickname = "";
			}
			
			dob = player.getString("dob");
			college = player.getString("college");
			lastName = player.getString("last_name");
			teams = this.parseTeams(player.getString("team"));
			drafted = player.getString("drafted");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private String[] parsePositions(String allPositions){
		return allPositions.split("/");
	}
	
	private String[] parseTeams(String allTeams){
		
		String[] words = allTeams.split(" ");
		ArrayList<String> myTeams = new ArrayList<String>();
		
		boolean cityFound = false;
		String city = null;
		
		for (int x = 0; x < words.length; x++){
			
			if(cityFound){
				myTeams.add(city+" "+words[x]);
				cityFound = false;
				continue;
			} else if(words[x].length() == 3){
				myTeams.add(words[x]);
			} else {
				city = words[x];
				cityFound = true;
			}
	
		}
		
		return myTeams.toArray(new String[myTeams.size()]);
	}
	
	public String toString(){
		String player = "Name: "+firstName+" "+lastName;
		return player;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Player.STATUS getStatus() {
		return status;
	}
	
	public String getStatusString(){
		if(status == STATUS.Active)
			return "A";
		else if (status == STATUS.Retired)
			return "R";
		else 
			return "?";
	}

	public void setStatus(Player.STATUS status) {
		this.status = status;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}
